#pragma once

#include "CharacterTypeInfo.h"

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Xml;


namespace lcabinet {

	/// <summary>
	/// Summary for NewCharacterDialog
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class NewCharacterDialog : public System::Windows::Forms::Form
	{
	public:
		NewCharacterDialog(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
			InitializeTypes();
		}

		String^ GetSelectedType()
		{
			if (lstTypeSelection->SelectedItem != nullptr)
			{
				return (lstTypeSelection->SelectedItem->ToString());
			}
			else
			{
				return (nullptr);
			}
		}

		XmlNode^ GetTypeNode()
		{
			String^ strSelectedItem = GetSelectedType();
			XmlNode^ pcSelectedType = nullptr;

			for each(XmlNode^ pcType in CharacterTypeInfo::GetInstance()->GetTypes())
			{
				String^ strTypeName = pcType->Attributes->GetNamedItem("Name")->Value;
				
				if (strTypeName == strSelectedItem)
				{
					pcSelectedType = pcType;
					break;
				}
			}
			return (pcSelectedType);
		}

		String^ GetSelectedTemplate()
		{
			if (lstTemplateSelection->SelectedItem != nullptr)
			{
				return (lstTemplateSelection->SelectedItem->ToString());
			}
			else
			{
				return (nullptr);
			}
		}

		XmlNode^ GetTemplateNode()
		{
			String^ strSelectedItem = GetSelectedTemplate();
			XmlNode^ pcSelectedTemplate = nullptr;

			for each(XmlNode^ pcTemplate in CharacterTypeInfo::GetInstance()->GetTemplates(GetTypeNode()))
			{
				String^ strTemplateName = pcTemplate->Attributes->GetNamedItem("Name")->Value;
				
				if (strTemplateName == strSelectedItem)
				{
					pcSelectedTemplate = pcTemplate;
					break;
				}
			}
			return (pcSelectedTemplate);
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~NewCharacterDialog()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::ListBox^  lstTypeSelection;
	private: System::Windows::Forms::ListBox^  lstTemplateSelection;
	private: System::Windows::Forms::Button^  btnOk;
	private: System::Windows::Forms::Button^  btnCancel;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	protected: 

	protected: 




	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->lstTypeSelection = (gcnew System::Windows::Forms::ListBox());
			this->lstTemplateSelection = (gcnew System::Windows::Forms::ListBox());
			this->btnOk = (gcnew System::Windows::Forms::Button());
			this->btnCancel = (gcnew System::Windows::Forms::Button());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// lstTypeSelection
			// 
			this->lstTypeSelection->FormattingEnabled = true;
			this->lstTypeSelection->Location = System::Drawing::Point(12, 25);
			this->lstTypeSelection->Name = L"lstTypeSelection";
			this->lstTypeSelection->Size = System::Drawing::Size(120, 95);
			this->lstTypeSelection->TabIndex = 0;
			this->lstTypeSelection->SelectedIndexChanged += gcnew System::EventHandler(this, &NewCharacterDialog::SelectTypes);
			// 
			// lstTemplateSelection
			// 
			this->lstTemplateSelection->FormattingEnabled = true;
			this->lstTemplateSelection->Location = System::Drawing::Point(170, 25);
			this->lstTemplateSelection->Name = L"lstTemplateSelection";
			this->lstTemplateSelection->Size = System::Drawing::Size(120, 95);
			this->lstTemplateSelection->TabIndex = 1;
			this->lstTemplateSelection->SelectedIndexChanged += gcnew System::EventHandler(this, &NewCharacterDialog::SelectTemplate);
			// 
			// btnOk
			// 
			this->btnOk->DialogResult = System::Windows::Forms::DialogResult::OK;
			this->btnOk->Enabled = false;
			this->btnOk->Location = System::Drawing::Point(134, 126);
			this->btnOk->Name = L"btnOk";
			this->btnOk->Size = System::Drawing::Size(75, 23);
			this->btnOk->TabIndex = 2;
			this->btnOk->Text = L"Ok";
			this->btnOk->UseVisualStyleBackColor = true;
			// 
			// btnCancel
			// 
			this->btnCancel->DialogResult = System::Windows::Forms::DialogResult::Cancel;
			this->btnCancel->Location = System::Drawing::Point(215, 126);
			this->btnCancel->Name = L"btnCancel";
			this->btnCancel->Size = System::Drawing::Size(75, 23);
			this->btnCancel->TabIndex = 3;
			this->btnCancel->Text = L"Cancel";
			this->btnCancel->UseVisualStyleBackColor = true;
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(9, 9);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(80, 13);
			this->label1->TabIndex = 4;
			this->label1->Text = L"Character Type";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(167, 9);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(51, 13);
			this->label2->TabIndex = 5;
			this->label2->Text = L"Template";
			// 
			// NewCharacterDialog
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(306, 162);
			this->ControlBox = false;
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->btnCancel);
			this->Controls->Add(this->btnOk);
			this->Controls->Add(this->lstTemplateSelection);
			this->Controls->Add(this->lstTypeSelection);
			this->Name = L"NewCharacterDialog";
			this->Text = L"Character Selection";
			this->ResumeLayout(false);
			this->PerformLayout();

		}

		System::Void InitializeTypes()
		{
			XmlNodeList^ pcTypeList = CharacterTypeInfo::GetInstance()->GetTypes();
			int nItems = pcTypeList->Count;

			try
			{
				for (int nIndex = 0; nIndex < nItems; nIndex++)
				{
					XmlNode^ pcType = pcTypeList->Item(nIndex);
					String^ strTypeName = pcType->Attributes->GetNamedItem("Name")->Value;

					lstTypeSelection->Items->Add(strTypeName);
				}
			}
			catch (Exception^)
			{
				MessageBox::Show("Error populating Type list");
			}
		}

#pragma endregion
		System::Void SelectTypes(System::Object^ sender, System::EventArgs^ e)
		{
			XmlNode^ pcSelectedType = GetTypeNode();

			try
			{
				if (pcSelectedType)
				{
					lstTemplateSelection->Items->Clear();
					btnOk->Enabled = false;

					for each (XmlNode^ pcTemplate in CharacterTypeInfo::GetInstance()->GetTemplates(pcSelectedType))
					{
						String^ strTemplateName = pcTemplate->Attributes->GetNamedItem("Name")->Value;

						lstTemplateSelection->Items->Add(strTemplateName);
					}
				}
			}
			catch (Exception^ e)
			{
				MessageBox::Show("Error populating Template list");
			}
		}

		System::Void SelectTemplate(System::Object^ sender, System::EventArgs^ e)
		{
			if (GetSelectedType() && GetSelectedTemplate())
			{
				btnOk->Enabled = true;
			}
			else
			{
				btnOk->Enabled = false;
			}
		}
	};
}
